/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

namespace hhd
{
    extern hhdByteOrder machineByteOrder();

    class OutputStream;
    class String;

    /**
    * DataOutputStream is used to write primitive types (and Hogshead Strings) to an OutputStream in
    * either Little Endian or Big Endian format.
    * Using the same Endianness as the machine you are running on will be faster. x86 and x64 are
    * Little Endian while RISC and PowerPC are Big Endian.
    * @see OutputStream
    */
    class DataOutputStream : public OutputStream
    {
        public:
            /**
            * This constructor takes in an OutputStream to be used.  It uses whatever the machine
            * endianness is.
            * @param in_stream OutputStream to write to.
            */
            DataOutputStream(OutputStream& in_stream);
            /**
            * This constructor takes in an OutputStream and the byte ordering to use.
            * @param in_stream OutputStream to write to.
            * @param in_order The ByteOrder to use (Little Endian and Big Endian)
            */
            DataOutputStream(OutputStream& in_stream, hhdByteOrder in_order);


            void writeChar(char in_c);
            void writeUShort(unsigned short in_us);
            void writeInt(int in_i);
            void writeUInt(unsigned int in_ui);
            void writeFloat(float in_f);
            void writeDouble(double in_d);
            /**
            * Writes a String object to the OutputStream in the following format:
            * int32 - length of string
            * char,char,char ... char - The characters which make up the String.
            */
            void writeString(const String& in_string);

            void flush();
            void write(char in_char);
            void write(const char* in_string);
            void write(const char* in_bytes, unsigned int in_count);
            void reset();

        private:
            template<typename T> void _write(T in_t)
            {
                char* buff = (char*)&in_t;
                if(_byte_order == machineByteOrder())
                {
                    _stream->write(buff, sizeof(T));
                }
                else
				{
                    for(int k = sizeof(T) - 1; k >= 0; k--)
                    {
                        _stream->write(buff[k]);
                    }
				}
            };
            OutputStream* _stream;
            hhdByteOrder _byte_order;
    };
}
